<template>
	<view class="container-c">
		<view class="card">
			<view class="top">
				<text>订单编号:{{backObj.orderSn}}</text>
			</view>
			<view class="middle">
				<image class="left" :src="backObj.productPic" mode=""></image>
				<view class="right">
					<view class="top-info">
						<text class="name">{{backObj.productName}}</text>
						<text class="price">¥{{backObj.productPrice}}</text>
					</view>
					<view class="middle-info">
						<text class="">编号{{backObj.orderId}}</text>
						<text class="price">x{{backObj.productQuantity}}</text>
					</view>
					<view class="bottom-info" v-for="ele in productAttr" :key="ele.id">
						{{ele.key}}:{{ele.value}}
					</view>
				</view>
			</view>
			<view class="total">
				合计:{{backObj.totalPrice}}元
			</view>
		</view>
		<form @submit="formSubmit">
			<view class="returninfo">
				<view class="item">
					<view class="left">
						请选择退单数量
					</view>
					<view class="right">
						1
					</view>
				</view>
				<view class="item">
					<text class="left">请输入退货原因:</text>
					<input class="input" placeholder-style="font-size:24rpx" type="text" name="reason"
						placeholder="请输入退货原因" />
				</view>
				<view class="item">
					<text class="left">请选择收货地址</text>
					<picker  range-key='addressName' :value="cityvalue" :range="city"  @change="seleteAddress">
						<view>{{city[cityvalue]?city[cityvalue].addressName:'123'}}</view>
					</picker>
					<!-- <text class="right" @tap="openAddress">请选择收货地址</text> -->
				</view>
				<view class="textarea">
					<text>请输入问题描述:</text>
					<textarea class="content" name="description" placeholder-style="font-size:24rpx" placeholder="请输入问题描述" />
				</view>
				<view class="point">
					<view class="title">
						温馨提醒:
					</view>
					<view class="content">
						上传图片总大小不能超过3m，单个文件大小不能超过1m，图片总个数不能超过3张
					</view>
				</view>
				<view class="upImg">
					<view class="">
						<view class="add" @tap="upImage">
							+
						</view>
						<view class="text">
							上传图片
						</view>
					</view>
					<view class="images" v-for="item in imgList">
						<image :src="item" mode=""></image>
					</view>
				</view>
			</view>
			<button class="imgbtn" @tap="submit" formType="submit" type="default">提交</button>
		</form>

	</view>
</template>

<script>
	import {
		getCompanyAddrss,addOrderReturnApply
	} from "@/api/user/order/index.js"
	import baseUrl from "@/api/baseUrl.js"
	import {
		getPreOrderById
	} from "@/api/list/order/index.js"
	export default {
		data() {
			return {
				backObj: {},
				productAttr: [],
				imgList: [],
				inptuValue:{},
				orderId:"",
				orderItemId:"",
				city:[],
				index:0,
				cityvalue:0,
				companyAddressId:""
				
			};
		},

		onLoad(e) {
			console.log(e)
			this.orderId=e.orderId
			
			getPreOrderById(e.id).then(res => {
				console.log(res)
				this.backObj = res.data.orderItems[0]
				var arr = res.data.orderItems[0].productAttr;
				this.productAttr = JSON.parse(arr);
				console.log(this.productAttr)
				this.orderItemId=res.data.orderItems[0].id
			})
			getCompanyAddrss().then(res => {
				console.log(res)
				this.city=res.data.items	
						console.log(	this.city);
				// arr.forEach(ele=>this.city.push(ele.addressName))
			})
			console.log(this.city)

		},
		methods: {
			seleteAddress(index){
				console.log(index)
				console.log(this.city[this.cityvalue]);
				this.companyAddressId=this.city[this.cityvalue].id
				// console.log(this.companyAddressId)		
			},

			// 提交表单
			formSubmit(e) {
			console.log(e)
			this.inptuValue=e.detail.value
			},
			onConfirm() {},
			upImage() {
				var _this = this;
				uni.chooseImage({
					success(res) {
						console.log(res)
						if (res.tempFilePaths.length > 3) {
							uni.showToast({
								title: "单次选择图片不能超过三张",
								icon: "none"
							})
							return
						}
						if (res.tempFilePaths.length + _this.imgList.length > 3) {
							uni.showToast({
								title: "图片总数不能超过三张",
								icon: "none"
							})
							return
						}
						var item = res.tempFilePaths.find(ele => ele.size / 1024 / 1024 > 1);
						if (item) {
							uni.showToast({
								title: "选取的图片不能超过1M,请重新选择",
								icon: "none"
							})
						}
						_this.imgList.push(...res.tempFilePaths);
						console.log(_this.imgList)
					}
				})
			},
			submit() {
				var arr = [],
					_this = this;
				this.imgList.forEach(ele => {
					// Promise实例处理异步
					var pro = new Promise((resolve, rejected) => {
						uni.uploadFile({
							url: baseUrl + "/lejuClient/orderReturn/uploadImg",
							filePath: ele,
							name: "file",
							success(res) {
								console.log(res)
								resolve(JSON.parse(res.data).data.fileUrl)
							}
						})

					})
					arr.push(pro)
					
				})
				var p1 = Promise.all(arr);
				p1.then(res => {
					var obj = {
						"companyAddressId": this.companyAddressId,
						"count": 1,
						"description": this.inptuValue.description,
						"orderId": this.orderId,
						"orderItemId":this.orderItemId,
						"proofPics":res.join() ,
						"reason": this.inptuValue.reason
					}
					addOrderReturnApply(obj).then(res=>{
						console.log(res)
					})
				})
				
			}
		}

	}
</script>

<style lang="scss" scoped>
	.container-c {
		width: 670rpx;
		margin: 0 auto;
	}

	.card {
		background-color: #FFFFFF;
		border-radius: 24rpx;
		padding: 36rpx;
		margin-top: 40rpx;

		.top {
			border-bottom: 2rpx solid #f1ece7;
			font-size: 24rpx;
			display: flex;
			justify-content: space-between;
			padding-bottom: 36rpx;
		}

		.middle {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #f1ece7;
			padding-bottom: 36rpx;

			image {
				width: 162rpx;
				height: 162rpx;
				// margin-right: 20rpx;
			}

			.right {
				.name {
					width: 280rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.top-info,
				.middle-info {
					display: flex;
					justify-content: space-between;
				}

				.top-info {
					font-size: 26rpx;
					line-height: 74rpx;
				}

				.middle-info,
				.bottom-info {
					font-size: 24rpx;
					color: #8d8d8d;
				}
			}
		}
	}

	.total {
		margin-left: 350rpx;
		font-size: 26rpx;
		line-height: 65rpx;
	}

	.returninfo {
		background-color: #fff;
		margin-top: 30rpx;
		padding: 0 30rpx;

		.item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 24rpx;
			border-bottom: 2rpx solid #eee;

			.input {
				text-align: right;
			}
		}

		.textarea {
			display: flex;
			justify-content: space-between;

			border-bottom: 2rpx solid #eee;
			font-size: 24rpx;
			padding-top: 30rpx;

			.content {
				width: 300rpx;
				// text-align: right;
			}
		}
	}

	.point {
		margin-top: 40rpx;
		font-size: 24rpx;

		.title {
			font-weight: 700;
		}
	}

	.upImg {
		padding-bottom: 30rpx;
		display: flex;

		.add {
			font-size: 80rpx;
		}

		.text {
			font-size: 24rpx;
		}
	}

	.imgbtn {
		width: 70%;
		height: 80rpx;
		border-radius: 20rpx;
		background-color: #354e44;
		color: #fff;
		line-height: 80rpx;
		text-align: center;
		font-size: 30rpx;
		margin: 50rpx auto;
	}

	.images {
		image {
			width: 120rpx;
			height: 120rpx;
		}
	}
</style>
